local filelog = require "filelog"

require "pjenum"

-------------------------

local CardTool = { }

-----------------------------------

--按从大到小排序
function CardTool.sort(cards)
	if cards[1] < cards[2] then
		cards[1], cards[2] = cards[2], cards[1]
	end
	return cards
end

--转换牌数据
function CardTool.convert(card)
	if not card then return 0 end
	local red = math.floor(card / 100)
	local black = card % 100
	if black > 40 then
		local b1 = math.floor(black / 10)
		local b2 = black % 10
		black = b1 + b2
	end
	return red + black
end
-- 计算牌值
function CardTool.getvalue(cards)
	if not cards or #cards ~= 2 then return 0 end
	local totalval = CardTool.convert(cards[1]) + CardTool.convert(cards[2])
	return totalval % 10
end
-- 获取牌类型
function CardTool.gettype(cards)
	if not cards or #cards ~= 2 then return false end
	if cards[1] == cards[2] then
		return EPJCardWithType[cards[1]]
	else
		cards = CardTool.sort(cards)
		if cards[1] == 402 and cards[2] == 102 then	-- 二四+丁三(大小猴)
			return EPJCardsType.CARDS_TYPE_ZHIZUN
		elseif cards[1] == 606 then
			if cards[2] == 9 then
				return EPJCardsType.CARDS_TYPE_TIANWANG
			elseif cards[2] == 44 then
				return EPJCardsType.CARDS_TYPE_TIANGANG
			elseif cards[2] == 106 then
				return EPJCardsType.CARDS_TYPE_TIANGAOJIU
			end
		elseif cards[1] == 200 then	-- 这里利用一个巧合简化了流程
			if cards[2] == 9 then
				return EPJCardsType.CARDS_TYPE_DIWANG
			elseif cards[2] == 44 then
				return EPJCardsType.CARDS_TYPE_DIGANG
			elseif cards[2] == 106 then
				return EPJCardsType.CARDS_TYPE_DIGAOJIU
			end
		else
			local cv1 = CardTool.convert(cards[1])
			local cv2 = CardTool.convert(cards[2])
			if cv1 == cv2 then
				if cv1 == 9 then
					return EPJCardsType.CARDS_TYPE_SHUANGZAJIU
				elseif cv1 == 8 then
					return EPJCardsType.CARDS_TYPE_SHUANGZABA
				elseif cv1 == 7 then
					return EPJCardsType.CARDS_TYPE_SHUANGZAQI
				elseif cv1 == 5 then
					return EPJCardsType.CARDS_TYPE_SHUANGZAWU
				end
			end
		end
	end
	return EPJCardsType.CARDS_TYPE_DIANSHUPAI
end
-- 比较牌大小
-- 0 和		1 闲家赢	2 庄家赢
function CardTool.compare(cards1, cards2)
	if not cards1 or not cards2 then return end
	cards1 = CardTool.sort(cards1)
	cards2 = CardTool.sort(cards2)
	local ct1 = CardTool.gettype(cards1)
	local ct2 = CardTool.gettype(cards2)
	if ct1 == ct2 then	-- 只有点数牌是同类型
		local csv1 = CardTool.getvalue(cards1)
		local csv2 = CardTool.getvalue(cards2)
		if csv1 > csv2 then
			return 2
		elseif csv1 < csv2 then
			return 1
		else
			if csv1 == 0 then return 0 end	-- 如果双方都为0点，则为和
			local cv11, cv12 = EPJCardsCompareValue[cards1[1]], EPJCardsCompareValue[cards1[2]]
			local cv21, cv22 = EPJCardsCompareValue[cards2[1]], EPJCardsCompareValue[cards2[2]]
			local cv1 = cv11 > cv12 and cv11 or cv12
			local cv2 = cv21 > cv22 and cv21 or cv22
			if cv1 > cv2 then
				return 2
			elseif cv1 < cv2 then
				return 1
			else
				return 0
			end
		end
	else
		local ctv1 = EPJCardsTypeValue[ct1]
		local ctv2 = EPJCardsTypeValue[ct2]
		if ctv1 == nil or ctv2 == nil then
			filelog.sys_error("---------->", ct1, ct2, cards1, cards2)
		end
		if ctv1 > ctv2 then
			return 2
		elseif ctv1 < ctv2 then
			return 1
		else
			return 0
		end
	end
end

return CardTool
